package com.youbug.mall.ware_wms.service.impl;

import com.youbug.common.vo.FareVo;
import com.youbug.common.vo.MemberAddressVo;
import com.youbug.mall.ware_wms.entity.WmsWareInfo;
import com.youbug.mall.ware_wms.feign.IWareMemberFeignService;
import com.youbug.mall.ware_wms.mapper.WmsWareInfoMapper;
import com.youbug.mall.ware_wms.service.IWmsWareInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import com.youbug.common.vo.QueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;

/**
 * <p>
 * 仓库信息 服务实现类
 * </p>
 *
 * @author youbug
 * @since 2024-05-01
 */
@Service
public class WmsWareInfoServiceImpl extends ServiceImpl<WmsWareInfoMapper, WmsWareInfo> implements IWmsWareInfoService {

        @Autowired
        private IWareMemberFeignService wareMemberFeignService;

        @Override
        public IPage<WmsWareInfo> getWmsWareInfoPage(QueryVo<WmsWareInfo> queryVo) {

        Page<WmsWareInfo> paramPage = new Page<>();

        paramPage.setCurrent(queryVo.getCurrentPage());

        paramPage.setSize(queryVo.getPageSize());

        QueryWrapper<WmsWareInfo> wrapper = new QueryWrapper<>();

        WmsWareInfo wmsWareInfo = queryVo.getPayload();

        if(!ObjectUtils.isEmpty(wmsWareInfo)){
            if(StringUtils.hasText(wmsWareInfo.getName())){
                wrapper.eq("name", wmsWareInfo.getName());
            }
            if(StringUtils.hasText(wmsWareInfo.getAddress())){
                wrapper.eq("address", wmsWareInfo.getAddress());
            }
            if(StringUtils.hasText(wmsWareInfo.getAreacode())){
                wrapper.eq("areacode", wmsWareInfo.getAreacode());
            }
        }
        if(!ObjectUtils.isEmpty(queryVo.getStartTime())){
            wrapper.gt("create_time", queryVo.getStartTime());
        }
        if(!ObjectUtils.isEmpty(queryVo.getEndTime())){
            wrapper.lt("create_time", queryVo.getEndTime());
        }
        wrapper.orderByDesc("create_time");

        Page<WmsWareInfo> page = this.page(paramPage, wrapper);

        return page;
        }

    @Override
    public FareVo getFare(Long addrId) {

        MemberAddressVo addrInfo = wareMemberFeignService.addrInfo(addrId);

        if(addrInfo!=null){

            FareVo fareVo = new FareVo();
            String phone = addrInfo.getPhone();

            String fare = phone.substring(phone.length() - 1);// 模拟计算运费
            fareVo.setFare(new BigDecimal(fare));
            fareVo.setAddress(addrInfo);
            return fareVo;
        }
        return null;
    }

       /* @Transactional(transactionManager = "transactionManagerXXX", rollbackFor = Exception.class)
        public boolean saveBatch(Collection<WmsWareInfo> entityList) {
        return saveBatch(entityList, DEFAULT_BATCH_SIZE);
        }
        */

}
